package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.CollectionDto;
import com.heima.article.entity.ApCollection;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.feign.BehaviorFeign;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author GQ
 * @since 2023-07-14
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {
    @Autowired
    private BehaviorFeign behaviorFeign;
//    private IApBehaviorEntryService apBehaviorEntryService;

    /**
     * APP端文章收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveCollection(CollectionDto dto) {
        User user = UserThreadLocalUtil.get();
        ApBehaviorEntry entry = behaviorFeign.getEntry(user.getUserId(), dto.getEquipmentId());
        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApCollection::getEntryId, dto.getEquipmentId());
        wrapper.eq(ApCollection::getArticleId, dto.getArticleId());
        ApCollection one = this.getOne(wrapper);
        if (one == null) {
//            第一次收藏
            ApCollection apCollection = new ApCollection();
            apCollection.setEntryId(entry.getId());
            apCollection.setArticleId(dto.getArticleId());
            boolean save = this.save(apCollection);
            if (save) {
                return ResponseResult.okResult();
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }
        }
        return null;
    }
}
